import { useAppDispatch, useAppSelector } from '@/hooks/useStore';
import { login } from '@/store/reducer/user';
import { useNavigate } from 'react-router-dom';

interface Props {
  string?: string;
}
const Login: React.FC<Props> = () => {
  const navigate = useNavigate();
  const dispatch = useAppDispatch();

  const handleLogin = e => {
    // 阻止 onSubmit 刷新的默认事件
    e.preventDefault();
    // 模拟登录请求
    dispatch(login());

    // 读取保存的路径
    const preRoute = localStorage.getItem('preRoute');
    if (preRoute && preRoute !== '/login') {
      navigate(preRoute); // 跳转回原路径
    } else {
      navigate('/'); // 默认跳转首页
    }
  };

  return (
    <div>
      请登录
      <br />
      <form onSubmit={handleLogin}>
        {/* 登录表单 */}
        <button type='submit'>登录</button>
      </form>
    </div>
  );
};

export default Login;
